<template>
  <div class="login">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item>
        <img src="@/assets/common/login-logo.png" alt="">
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          placeholder="请输入手机号"
          prefix-icon="el-icon-user-solid"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入手机号"
          prefix-icon="el-icon-lock"
          show-password
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { sysLogin } from '@/api/user.js'
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '密码在6~12位之间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // const res = await sysLogin(this.form)
          // this.$store.commit('user/setToken', res.data)
          // console.log(res)
          try {
            await this.$store.dispatch('user/toLogin', this.form)
            this.$message.success('登录成功')
            this.$router.push('/')
          } catch {}

          // this.$router.push('/')
        }
        // console.log(result)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .btn {
    width: 100%;
    height: 60px;
  }
  ::v-deep .el-input__inner {
    height: 52px;
  }
}
</style>
